package com.zhht.checkcop.parser;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import com.zhht.checkcop.db.MysqlDb;
import com.zhht.checkcop.db.StaticDBMap;

public class BaiduParserMovie implements Pipeline {

	private String novelUrl;

	private String type;

	public BaiduParserMovie() {
	}

	public BaiduParserMovie(String type, String novelUrl) {
		this.type = type;
		this.novelUrl = novelUrl;
	}

	public void process(ResultItems resultItems, Task arg1) {
		Map<String, Object> mapResults = resultItems.getAll();
		Iterator<Entry<String, Object>> iter = mapResults.entrySet().iterator();
		Map.Entry<String, Object> entry;

		List<String> frontCoverUrl = new ArrayList<String>();
		List<String> infoUrl = new ArrayList<String>();
		List<String> infoPicUrl = new ArrayList<String>();
		String classifyId = "";
		String infoClassifyName = "";

		// 输出到控制台
		while (iter.hasNext()) {
			entry = iter.next();
//			if (entry.getKey().equals("frontCoverUrl")) {
//				frontCoverUrl.addAll((Collection<? extends String>) entry.getValue());
//			}
			if (entry.getKey().equals("infoUrl")) {
				infoUrl.addAll((Collection<? extends String>) entry.getValue());
			}
			if (entry.getKey().equals("infoPicUrl")) {
				infoPicUrl.addAll((Collection<? extends String>) entry.getValue());
			}
		}

		if (null != infoUrl && infoUrl.size() > 0) {
			Map<String, Object> paramMap = new HashMap<String, Object>();
//			paramMap.put("frontCoverUrl", frontCoverUrl);
			paramMap.put("infoUrl", infoUrl);
			paramMap.put("infoPicUrl", infoPicUrl);
			try {
				StaticDBMap.mysqldb.execute("update t_gf_authors set baidu_info = '"
						+ (infoUrl.size()>0?infoUrl.get(0):"") + "',img='" + (infoPicUrl.size()>0?infoPicUrl.get(0):"")
						+ "' where name = '" + type+"'");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}
